package com.cndongtai.controller.user;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


















import com.cndongtai.constant.LightConstant;
import com.cndongtai.constant.SystemConstant;
import com.cndongtai.constant.UserConstant;
import com.cndongtai.controller.BaseController;
import com.cndongtai.net.redis.RedisServer;
import com.cndongtai.po.Dtu;
import com.cndongtai.po.Light;
import com.cndongtai.po.Menu;
import com.cndongtai.po.User;
import com.cndongtai.service.light.DtuService;
import com.cndongtai.service.light.LightService;
import com.cndongtai.service.user.UserService;
import com.cndongtai.util.DateUtil;
import com.cndongtai.util.MD5;
import com.cndongtai.util.ValidateUtil;
import com.cndongtai.vo.MenuBean;

@Controller
public class LoginController extends BaseController {

	@Resource(name="userService")
	private UserService userService;
	
	@Resource(name="lightService")
	private LightService lightService;
	
	@Resource(name="dtuService")
	private DtuService dtuService;
	
	private RedisServer redis = RedisServer.getInstance();
	
	@RequestMapping("/login")
	public String login() {
		String userName = this.getValue("username");
		String pwd = this.getValue("pwd");
		
		if(ValidateUtil.notEmpty(userName) && ValidateUtil.notEmpty(pwd)) {
			User user = userService.getUserInfoByUsername(userName);
			
			if(user!=null) {
				String pwdStr = new MD5().getMD5ofStr(pwd);
				
				if(pwdStr.equals(user.getPassword())) {
					user.setPassword("");
					request.getSession().setAttribute(UserConstant.SESSION_USER, user);
					
					return "redirect:/main.do";
				}
			}
		}
		
		// 返回提示信息到页面
		request.setAttribute("userName", userName);
		request.setAttribute("msg", getMessage("login.msg.error",null)+"！");
		
		return "/login";
	}
	
	@RequestMapping("/main")
	public String main() {
		// 日期
		request.setAttribute("nowdate", DateUtil.timeShortFormatCN(new Date()));
		
		User user = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
		
		// 获取DTU集合
		List<Dtu> dtuList = null;
		if("admin".equals(user.getUsername())) {
			dtuList = dtuService.queryDtuList();
		}else {
			dtuList = dtuService.queryDtuListByUserId(user.getUserId());
		}
		request.setAttribute("dtuList", dtuList);
		
		
		// 获取用户权限
		MenuBean menubean = new MenuBean();
		if(user.getUsername().equals("admin")) {
			menubean.setAdmin();
		}else {
			List<Menu> menuList = userService.queryMenuByUserId(user.getUserId());
			request.setAttribute("menuList", menuList);
			
			
			for(Menu menu : menuList) {
				if(menu.getMenuId()==1) {
					menubean.setMenu1(true);
				}else if(menu.getMenuId()==2) {
					menubean.setMenu2(true);
				}else if(menu.getMenuId()==3) {
					menubean.setMenu3(true);
				}else if(menu.getMenuId()==4) {
					menubean.setMenu4(true);
				}else if(menu.getMenuId()==5) {
					menubean.setMenu5(true);
				}else if(menu.getMenuId()==6) {
					menubean.setMenu6(true);
				}else if(menu.getMenuId()==7) {
					menubean.setMenu7(true);
				}
			}
		}
		request.getSession().setAttribute("menubean", menubean);
		
		StringBuffer locationJson = new StringBuffer();
		locationJson.append("[");
		
		for(Dtu dtu : dtuList) {
			List<Light> lightList = lightService.queryLightListByDtuId(dtu.getDtuId()+"");
			for(Light light : lightList) {
				
				// 获取路灯在线状态
				if(redis.get(LightConstant.REDIS_LIGHT_KEY+dtu.getPanId()+"_"+light.getLightNum())==null) {
					light.setOnlineState(1);
				}else {
					light.setOnlineState(0);
				}
				
				locationJson.append("{").append("lightId:").append(light.getLightId())
				.append(",state:").append(light.getOnlineState())
				.append(",title:").append("\"").append(light.getLightNum()).append("\"")
				.append(",content:").append("\"").append(light.getAddressName()).append("\"")
				.append(",point:").append("\"").append(light.getLongitude()).append("|").append(light.getLatitude()).append("\"")
				.append(",isOpen:").append(light.getLightState())
				.append(",icon:{w:60,h:70,l:0,t:0,x:6,lb:5}},");
			}
		}
		
		if(locationJson.length()>1) {
			locationJson.deleteCharAt(locationJson.length()-1);
		}
		
		locationJson.append("]");
		
		// 计算二氧化碳排放量
		String co2 = userService.queryCo2decreasedByUserId(user.getUserId());
		request.setAttribute("co2", co2);
		
		// 计算节能排名
		String co2Order = userService.queryCo2decreasedOrderByUserId(user.getUserId());
		request.setAttribute("co2Order", co2Order);
		
		request.setAttribute("locationJson", locationJson.toString());
		return "/mainpage";
	}
	
	@RequestMapping("/logout")
	public String logout() {
		String language = (String) request.getSession().getAttribute(SystemConstant.LANGUAGE);
		request.getSession().invalidate();
		if("zh".equals(language) || "en".equals(language)) {
			request.getSession().setAttribute(SystemConstant.LANGUAGE, language);
		}
		return "/login";
	}
	
	@RequestMapping("/loginpage")
	public String loginpage() {
		return "/login";
	}
	
}
